﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCTAccess.Business;
using DCTLib.Common;
using System.Data;

namespace DCTAccess.Business.History
{
    public class WebHistoryAdminFilter
    {
        #region Private members
        private int _historyID = -1;
        private int _adminID = -1;
        private int _historyTypeID = -1;
        private string _startDate = string.Empty;
        private string _endDate = string.Empty;
        private string _sortOrder = " Order by HistoryDate ";

        public string SortOrder
        {
            get { return _sortOrder; }
            set { _sortOrder = value; }
        }
        #endregion

        #region Public properties
        public int HistoryID
        {
            get { return _historyID; }
            set { _historyID = value; }
        }
        public int AdminID
        {
            get { return _adminID; }
            set { _adminID = value; }
        }
        public int HistoryTypeID
        {
            get { return _historyTypeID; }
            set { _historyTypeID = value; }
        }
        public string StartDate
        {
            get { return _startDate; }
            set { _startDate = value; }
        }
        public string EndDate
        {
            get { return _endDate; }
            set { _endDate = value; }
        }
        #endregion

        private void FilterCondition(ref SQLHelper sqlHelper, ref StringBuilder sql)
        {
            if (_historyID != -1)
            {
                sql.Append("AND " + IntegerCompareSQLBuilder.GetSQL("HistoryID", IntegerCompareTypes.Equals, "@HistoryID"));
                sqlHelper.AddParameter("@HistoryID", _historyID);
            }

            if (_adminID != -1)
            {
                sql.Append("AND " + IntegerCompareSQLBuilder.GetSQL("h.AdminID", IntegerCompareTypes.Equals, "@AdminID"));
                sqlHelper.AddParameter("@AdminID", _adminID);
            }

            if (_historyTypeID != -1)
            {
                sql.Append("AND " + IntegerCompareSQLBuilder.GetSQL("h.HistoryTypeID", IntegerCompareTypes.Equals, "@HistoryTypeID"));
                sqlHelper.AddParameter("@HistoryTypeID", _historyTypeID);
            }

            if (_startDate.Trim().Length > 0)
            {
                sql.Append("AND " + DateCompareSQLBuilder.GetSQL("HistoryDate", DateCompareTypes.AfterOrEqual, "@StartDate"));
                sqlHelper.AddParameter("@StartDate", _startDate);
            }

            if (_endDate.Trim().Length > 0)
            {
                sql.Append("AND " + DateCompareSQLBuilder.GetSQL("HistoryDate", DateCompareTypes.Before, "@EndDate"));
                sqlHelper.AddParameter("@EndDate", _endDate);
            }
        }

        private string SelectJoinSQL
        {
            get
            {
                StringBuilder str = new StringBuilder();
                str.Append("Select HistoryID,h.AdminID as 'AdminID',FullName,h.HistoryTypeID As 'HistoryTypeID',HistoryType,convert(varchar(30),HistoryDate,113) as'HistoryDate',Description ");
                str.Append("from dbo.WebHistory_Admin h Join dbo.Web_Admins a ");
                str.Append("On h.AdminID = a.AdminID Join dbo.WebSetting_HistoryTypes t ");
                str.Append("On h.HistoryTypeID = t.HistoryTypeID Where 1 = 1 ");
                return str.ToString();
            }
        }

        private string SelectAll
        {
            get
            {
                return "Select * from dbo.WebHistory_Admin Where 1 = 1 ";
            }
        }

        public DataTable GetJoinDataTable()
        {
            SQLHelper sqlHelper = new SQLHelper();
            sqlHelper.SwitchCommandType(CommandType.Text);
            StringBuilder sql = new StringBuilder(SelectJoinSQL);
            FilterCondition(ref sqlHelper, ref sql);
            sql.Append(_sortOrder);
            return sqlHelper.GetDataTable(sql.ToString());
        }

        public DataTable GetAllDataTable()
        {
            SQLHelper sqlHelper = new SQLHelper();
            sqlHelper.SwitchCommandType(CommandType.Text);
            StringBuilder sql = new StringBuilder(SelectAll);
            FilterCondition(ref sqlHelper, ref sql);
            sql.Append(_sortOrder);
            return sqlHelper.GetDataTable(sql.ToString());
        }
    }
}
